package com.emeraldparser.symbol;

import java.io.IOException;

import com.emeraldparser.util.Characters;

public class SimpleSymbolClassWriter implements SymbolClassWriter {

	private enum Event {
		CURSOR
	}

	private final Appendable appendable;
	private Event lastEvent;

	public SimpleSymbolClassWriter(Appendable appendable) {
		this.appendable = appendable;
	}

	private void append(CharSequence charSequence) {
		try {
			for (int i = 0; i < charSequence.length(); i++) {
				appendable.append(Characters.escape(charSequence.charAt(i)));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

	private void append(char character) {
		append(String.valueOf(character));
	}

	@Override
	public void writeCharacters(CharSequence characters) {
		// TODO Auto-generated method stub
		append(characters);
		lastEvent = null;
	}

	@Override
	public void writeCharacter(char character) {
		// TODO Auto-generated method stub
		append(character);
		lastEvent = null;
	}

	@Override
	public void writeStartGroup() {
		// TODO Auto-generated method stub
		append("(");
		lastEvent = null;
	}

	@Override
	public void writeEndGroup() {
		// TODO Auto-generated method stub
		append(")");
		lastEvent = null;
	}

	@Override
	public void writeStartCharacters() {
		// TODO Auto-generated method stub
		append("\"");
		lastEvent = null;
	}

	@Override
	public void writeEndCharacters() {
		// TODO Auto-generated method stub
		append("\"");
		lastEvent = null;
	}

	@Override
	public void writeStartCharacter() {
		// TODO Auto-generated method stub
		append("'");
		lastEvent = null;
	}

	@Override
	public void writeEndCharacter() {
		// TODO Auto-generated method stub
		append("'");
		lastEvent = null;
	}

	@Override
	public void writeCursor(boolean down, boolean up) {
		// TODO Auto-generated method stub
		if (lastEvent == null || lastEvent != Event.CURSOR) {
			if (down) {
				if (up) {
					append("&#x2195;");
				} else {
					append("&darr;");
				}
			} else {
				if (up) {
					append("&uarr;");
				} else {
					
				}
			}
		}
		
		lastEvent = Event.CURSOR;
	}

	@Override
	public void writeAlternationSymbol() {
		// TODO Auto-generated method stub
		append("|");
		lastEvent = null;
	}

}
